/*
 * Created by JFormDesigner on Sun Jun 04 18:38:43 CST 2023
 */

package gui.admin;

import dao.UserDao;
import entity.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class AddUserFrame extends JFrame {
    public AddUserFrame() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        userId = new JTextField();
        userName = new JTextField();
        identityCbox = new JComboBox();
        registerBtn = new JButton();
        cancelBtn = new JButton();
        label4 = new JLabel();
        userPassword = new JTextField();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7528\u6237\u754c\u9762");
        setVisible(true);
        setPreferredSize(new Dimension(600, 400));
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u7f16\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(115, 40, 65, 35);

        //---- label2 ----
        label2.setText("\u59d3\u540d");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(115, 90, 65, 35);

        //---- label3 ----
        label3.setText("\u7528\u6237\u8eab\u4efd");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label3);
        label3.setBounds(115, 140, 65, 35);
        contentPane.add(userId);
        userId.setBounds(235, 40, 230, 35);
        contentPane.add(userName);
        userName.setBounds(235, 90, 230, 35);
        contentPane.add(identityCbox);
        identityCbox.setBounds(235, 135, 80, 40);

        identityCbox.addItem("教师");
        identityCbox.addItem("学生");

        //---- registerBtn ----
        registerBtn.setText("添加");
        contentPane.add(registerBtn);
        registerBtn.setBounds(310, 285, 90, 45);

        registerBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String userIdTemp = userId.getText();
                String userNameTemp = userName.getText();
                String userPasswordTemp = userPassword.getText();
                int index = identityCbox.getSelectedIndex();
                String[] list = {"教师", "学生"};
                String userIdentityTemp = null;
                for (int i = 0; i < list.length; i++) {
                    if (i == index) {
                        userIdentityTemp = list[i];
                        break;
                    }
                }

                User user = new User(userIdTemp, userNameTemp, userPasswordTemp, 0.0F, userIdentityTemp);
                UserDao userDao = new UserDao();

                boolean result = false;
                try {
                    result = userDao.addUser(user);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                if (result) {
                    JOptionPane.showMessageDialog(null, "添加成功");
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败");
                }
            }
        });
        //---- cancelBtn ----
        cancelBtn.setText("\u53d6\u6d88");
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(445, 285, 90, 45);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //---- label4 ----
        label4.setText("\u7528\u6237\u5bc6\u7801");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label4);
        label4.setBounds(115, 185, 65, 35);
        contentPane.add(userPassword);
        userPassword.setBounds(235, 185, 230, 35);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField userId;
    private JTextField userName;
    private JComboBox identityCbox;
    private JButton registerBtn;
    private JButton cancelBtn;
    private JLabel label4;
    private JTextField userPassword;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) {
        AddUserFrame addUserFrame = new AddUserFrame();
    }
}
